package com.cloud.lan;

import com.cloud.lan.config.annotations.EnableOrderDal;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.time.LocalDateTime;

import javax.annotation.PostConstruct;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.net.NetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务启动
 * @author ErKang
 * @date Created in 2021/12/4 16:37
 * @title LancasterApplication.java
 * Department: Product development
 */
@Slf4j
@EnableAsync
@EnableTransactionManagement
@EnableOrderDal
@EnableDiscoveryClient
@RefreshScope
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class ManageApplication {

	@Value("${server.test}")
	private String serverTest;

	@Value("${spring.profiles.active}")
	private String active;

	public static void main(String[] args) {
		TimeInterval timer = DateUtil.timer();
		ConfigurableApplicationContext run = SpringApplication.run(ManageApplication.class, args);
		WebServer webServer = ((AnnotationConfigServletWebServerApplicationContext) run).getWebServer();
		log.info("Service Startup success：https://{}:{}", NetUtil.localIpv4s().iterator().next(), webServer.getPort());
		log.info("Spring boot 启动初始化了 {} 个 Bean, ->> 请求耗时：{}秒", run.getBeanDefinitionNames().length, timer.intervalSecond());
	}

	@PostConstruct
	private void init() {
		try {
			log.info("初始化logback打印info：{}", LocalDateTime.now());
			log.debug("初始化logback打印debug：{}", LocalDateTime.now());
			log.trace("初始化logback打印trace：{}", LocalDateTime.now());
			log.warn("初始化logback打印warn：{}", LocalDateTime.now());
			log.error("初始化logback打印error：{}", LocalDateTime.now());
			log.info("#初始化 环境 [{}],[{}]操作时间[{}]", active, serverTest, DateUtil.now());
		} catch (Exception e) {
			log.error("#初始化配置错误！");
		}
	}
}
